package com.gui.hospital.controller;

import com.gui.entity.R;
import com.gui.hospital.model.Hospital;
import com.gui.hospital.service.HospitalService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * @author 言叶
 * Date：2025/9/3
 */
@RestController //相当于@Controller + @ResponseBody
@RequestMapping("/hospital")
@AllArgsConstructor // 构造器自动注入
@CrossOrigin // 允许跨域
@Slf4j
public class HospitalController {
    private final HospitalService hospitalService;
    /*
     * 查询所有医院信息
     * @return
     */
    @GetMapping
    public R<List<Hospital>> findAll() {
        log.info("查询所有医院信息");
        List<Hospital> list = hospitalService.list();
        return new R<>(list);
    }
    /*
    根据医院名称模糊查询医院信息
     */
    @GetMapping("/findByNameHospital")
    public R<List<Hospital>> findByName(@RequestParam String name) {
        log.info("根据医院名称模糊查询医院信息");
        List<Hospital> list = hospitalService.findByName(name);
        return new R<>(list);
    }

    /*
    添加医院信息
     */
    @PostMapping("/save")
    public R<Hospital> save(@RequestBody Hospital hospital) {
        log.info("添加医院信息");
        boolean save = hospitalService.save(hospital);
        return save ? new R<>(hospital) : R.fail();

    }
    /*
    更新医院信息
     */
    @PutMapping("/update")
    public R<Hospital> update(@RequestBody Hospital hospital) {
        log.info("更新医院信息");
        boolean update = hospitalService.updateById(hospital);
        return update ? new R<>(hospital) : R.fail();
    }

    @DeleteMapping("/delete/{id}")
    public R<Hospital> delete(@PathVariable Integer id) {
        log.info("删除医院信息");
        boolean delete = hospitalService.removeById(id);
        return delete ? new R<>() : R.fail();
    }

    //给挂号微服务提供的查询医院信息接口（可能需要根据医院名称查询）
    @GetMapping("/findByName")
    public List<Hospital> list(@RequestParam(value = "name", required = false) String name) {
        log.info("给挂号微服务提供的查询医院信息接口，name是{}",name);
        return hospitalService.findByName(name);
    }

    /*
    根据医院id查询医院信息
     */
    @GetMapping("/getHospitalById/{id}")
    public Hospital getHospitalById(@PathVariable("id") Integer id) {
        log.info("根据医院id查询医院信息，id是{}",id);
        return hospitalService.getById(id);
    }

}
